package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.commons.exception.FoodException;
import com.woniuxy.commons.exception.code.FoodExceptionCode;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.controller.form.FoodVarietyForm;
import com.woniuxy.dao.FoodVarietyDao;
import com.woniuxy.dao.model.*;
import com.woniuxy.service.*;
import com.woniuxy.service.dto.FoodVarietyDto;
import com.woniuxy.service.dto.UserDetailsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Service
public class FoodVarietyServiceImpl extends ServiceImpl<FoodVarietyDao, FoodVariety> implements FoodVarietyService {
    @Resource
    private FoodVarietyDao foodVarietyDao;

    @Autowired
    private StoreInfoService storeInfoService;

    @Autowired
    private ProductsInfoService productsInfoService;

    @Autowired
    private ProductsFoodRelService productsFoodRelService;

    @Autowired
    private FoodMonthSaleService foodMonthSaleService;

    /**
     * @param keyword 菜品模糊查询关键字
     * @return 菜品传输对象
     */
    @Override
    public List<FoodVarietyDto> getFoodByKeyword(String keyword) {
        if (keyword == null || keyword.equals("") || keyword.equals("undefined")) {
            return BeanCopyUtil.copyList(list(), FoodVarietyDto.class);
        }
        QueryWrapper<StoreInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("storeName", keyword);
        List<StoreInfo> storeInfos = storeInfoService.list(queryWrapper);
        if (storeInfos == null || storeInfos.size() == 0) {
            QueryWrapper<ProductsInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.like("productsName", keyword);
            List<ProductsInfo> productsInfos = productsInfoService.list(queryWrapper1);
            if (productsInfos.size() > 0) {
                return BeanCopyUtil.copyList(foodVarietyDao.getFoodByProductName(keyword),
                        FoodVarietyDto.class);
            }
            QueryWrapper<FoodVariety> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.like("foodName", keyword);
            return BeanCopyUtil.copyList(list(queryWrapper2), FoodVarietyDto.class);
        }
        return BeanCopyUtil.copyList(foodVarietyDao.getFoodByStoreName(keyword), FoodVarietyDto.class);
    }

    /**
     * @param storeId    商家id
     * @param productsId 餐品id
     * @return 菜品传输对象
     */
    @Override
    public List<FoodVarietyDto> getFoodByStoreIdAndProductsId(Long storeId, Long productsId) {
        if (storeId != null) {
            if (storeInfoService.getById(storeId) == null)
                throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        } else {
            throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        }
        if (productsId != null) {
            if (productsInfoService.getById(productsId) == null)
                throw new FoodException(FoodExceptionCode.FOOD_NOT_PRODUCT);
        } else {
            throw new FoodException(FoodExceptionCode.FOOD_NOT_PRODUCT);
        }

        return BeanCopyUtil.copyList(foodVarietyDao
                        .getFoodByStoreIdAndProductsId(storeId, productsId),
                FoodVarietyDto.class);
    }

    /**
     * @param foodVarietyForm 菜品数据接受对象
     * @return 是否新增成功
     */
    @Override
    public boolean addFood(FoodVarietyForm foodVarietyForm) {
        if (foodVarietyForm.getStoreId() == null)
            throw new FoodException(FoodExceptionCode.FOOD_NOT_STOREDID);
        if (foodVarietyForm.getProductsId() == null)
            throw new FoodException(FoodExceptionCode.FOOD_NOT_PRODUCT);
        if (storeInfoService.getById(foodVarietyForm.getStoreId()) == null)
            throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        if (foodVarietyForm.getFoodName() == null
                || foodVarietyForm.getFoodName().equals(""))
            throw new FoodException(FoodExceptionCode.FOOD_NOT_NAME);
        if (foodVarietyForm.getPrice() == null || foodVarietyForm.getPrice() < 0)
            throw new FoodException(FoodExceptionCode.FOOD_NOT_PRICE);
        if (foodVarietyForm.getCoverPhoto() == null
                || foodVarietyForm.getCoverPhoto().equals(""))
            throw new FoodException(FoodExceptionCode.FOOD_NOT_PHOTO);
        FoodVariety entity = BeanCopyUtil.copyOne(foodVarietyForm, FoodVariety.class);
        save(entity);
        ProductsFoodRel productsFoodRel = new ProductsFoodRel();
        productsFoodRel.setProductsId(foodVarietyForm.getProductsId());
        productsFoodRel.setFoodId(entity.getId());
        productsFoodRelService.save(productsFoodRel);
        FoodMonthSale foodMonthSale = new FoodMonthSale();
        foodMonthSale.setStoreId(entity.getStoreId());
        foodMonthSale.setFoodId(entity.getId());
        foodMonthSaleService.save(foodMonthSale);
        return true;
    }

    @Override
    public FoodVarietyDto updateFood(Long id, FoodVarietyForm foodVarietyForm) {
        UpdateWrapper<FoodVariety> updateWrapper = null;
        if (foodVarietyForm.getStoreId() == null)
            throw new FoodException(FoodExceptionCode.FOOD_NOT_STOREDID);
        if (storeInfoService.getById(foodVarietyForm.getStoreId()) == null)
            throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        if (foodVarietyForm.getFoodName() == null
                || foodVarietyForm.getFoodName().equals(""))
            throw new FoodException(FoodExceptionCode.FOOD_NOT_NAME);
        if (foodVarietyForm.getPrice() == null || foodVarietyForm.getPrice() < 0)
            throw new FoodException(FoodExceptionCode.FOOD_NOT_PRICE);
        if (foodVarietyForm.getCoverPhoto() == null
                || foodVarietyForm.getCoverPhoto().equals(""))
            throw new FoodException(FoodExceptionCode.FOOD_NOT_PHOTO);
        if (id != null) {
            updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("storeId", foodVarietyForm.getStoreId());
            updateWrapper.set("foodName", foodVarietyForm.getFoodName());
            updateWrapper.set("synopsis", foodVarietyForm.getSynopsis());
            updateWrapper.set("detailInfo", foodVarietyForm.getDetailInfo());
            updateWrapper.set("price", foodVarietyForm.getPrice());
            updateWrapper.set("coverPhoto", foodVarietyForm.getCoverPhoto());
        } else {
            throw new FoodException(FoodExceptionCode.FOOD_NOT_ID);
        }
        updateWrapper.eq("id", id);
        update(updateWrapper);
        return BeanCopyUtil.copyOne(getById(id), FoodVarietyDto.class);
    }


    @Override
    public Page<FoodVariety> getMyFood(Long productsId, Long pageNum, Long pageSize) {
        UserDetailsDto userDetailsDto = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Long storeId = userDetailsDto.getId();

        if (storeId != null) {
            if (storeInfoService.getById(storeId) == null)
                throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        } else {
            throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        }
        if (productsId != null) {
            if (productsInfoService.getById(productsId) == null)
                throw new FoodException(FoodExceptionCode.FOOD_NOT_PRODUCT);
        } else {
            throw new FoodException(FoodExceptionCode.FOOD_NOT_PRODUCT);
        }


        Page<FoodVariety> myFood = foodVarietyDao.getMyFood(new Page<FoodVariety>(pageNum, pageSize), storeId, productsId);

        return myFood;

    }

    @Override
    public Page<FoodVariety> getAllMyFood(Long pageNum, Long pageSize) {

        UserDetailsDto userDetailsDto = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Long storeId = userDetailsDto.getId();

        if (storeId != null) {
            if (storeInfoService.getById(storeId) == null)
                throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        } else {
            throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        }

        Page<FoodVariety> myFood = foodVarietyDao.getAllMyFood(new Page<FoodVariety>(pageNum, pageSize), storeId);

        return myFood;
    }

    /**
     * @param storeId    商家id
     * @param productsId 餐品id
     * @return 菜品传输对象集合
     */
    @Override
    public List<FoodVarietyDto> getFoodByStoreIdAndProductsIdLimit8(Long storeId, Long productsId) {
        if (storeId != null) {
            if (storeInfoService.getById(storeId) == null)
                throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        } else {
            throw new FoodException(FoodExceptionCode.FOOD_NOT_STORED);
        }
        if (productsId != null) {
            if (productsInfoService.getById(productsId) == null)
                throw new FoodException(FoodExceptionCode.FOOD_NOT_PRODUCT);
        } else {
            throw new FoodException(FoodExceptionCode.FOOD_NOT_PRODUCT);
        }

        return foodVarietyDao
                .getFoodByStoreIdAndProductsId2(storeId, productsId);
    }
}
